<?php

namespace App\User\Services;

use App\Http\Models\UserAuth;
use App\Http\Services\ImService;

use App\User\Models\UserIdCard;
use App\BaseService;
use App\Exceptions\ApiException;
use App\User\Models\User;
use App\Common\Services\SmsService;
use Illuminate\Http\JsonResponse;

class UserService extends BaseService
{

    /**
     * User: Virus
     * DateTime: 2021/6/11 16:08
     * Describe: 查询用户信息
     * Version: v1.0.0
     * @return JsonResponse
     */
    public function getInfo()
    {
        $condition = request()->input('condition');
        $user = User::where(function ($table) use ($condition){
            $table->orWhere('id', $condition);
            $table->orWhere('username', $condition);
            $table->orWhere('id_card', $condition);
        })->first();
        return $this->success('success', $user);
    }

    /**
     * User: Virus
     * DateTime: 2021/6/11 16:06
     * Describe: 更新用户信息
     * Version: v1.0.0
     * @return JsonResponse
     */
    public function updateAuth()
    {
        $data = request()->input();
        try{
            // 查询用户
            $user = User::find($data['id']);
            if(!$user){
                throw new ApiException('用户不存在！');
            }
            // 验证身份证是否已经通过认证
            $id_card = UserIdCard::where([
                'real_name' => $data['real_name'],
                'id_card' => $data['id_card'],
                'status' => 1
            ])->first();
            if(!$id_card){
                throw new ApiException('身份证号未通过认证！');
            }
            // 修改用户信息
            $user->realname = $data['real_name'];
            $user->id_card = $data['id_card'];
            if(!$user->save()){
                throw new ApiException('修改用户认证信息失败！');
            }
            return $this->success('success', $user);
        }catch (ApiException $exception){
            return $this->success($exception->getMessage());
        }

    }

    /**
     * User: zhangchaoqun
     * DateTime: 2021/6/15 4:07 下午
     * Describe: 更新用户手机号
     * Version: V1.0
     */
    public function updatePhone() {
        $phone = request()->input('phone');
        $uId = request()->input('u_id');

        try {
            // 校验用户数据
            $userData = User::find($uId);
            if (!$userData) {
                throw new \Exception('用户不存在');
            }

            // 校验手机号是否被他人绑定
            $existPhone = User::where('phone', '=', $phone)->exists();
            if ($existPhone) {
                throw new \Exception('该手机号已被绑定，请勿重复绑定');
            }

            $userData->phone = $phone;
            if (!$userData->save()) {
                throw new \Exception('更新失败，请联系客服人员');
            }
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }

        return $this->success('更新成功');
    }

    /**
     * User: zhangchaoqun
     * DateTime: 2021/6/10 6:42 下午
     * Describe: 用户根据手机号登录
     * Version: V1.0
     * @return JsonResponse
     */
    public function loginByPhone() {
        $phone = request()->input('phone');

        try {
            // 校验用户数据
            $userData = User::where('phone', '=', $phone)
                ->select('id', 'phone', 'username','realname', 'avatar')
                ->first();

            if (!$userData) {
                // 不存在用户直接注册
                $newId = User::insertGetId([
                    'password' => '123456',
                    'phone' => $phone,
                    'created_at' => date('Y-m-d H:i:s'),
                    'updated_at' => date('Y-m-d H:i:s')
                ]);
                if (!$newId) {
                    throw new \Exception('登录失败，请联系客服人员');
                } else {
                    $userData = User::find($newId);
                }
            }

        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }

        return $this->success('登录成功', $userData);
    }

    /**
     * User: 孟郝泽
     * DateTime: 2021/6/11 2:49 下午
     * Describe: 腾讯IM 患者端用户
     * Version: V1.0
     * @return \Illuminate\Http\JsonResponse
     */
    public function userAuth() {

        $id = request()->input('id');
        $where = [
            'user_id'       => $id,
            'identity_type' => 'tencent_im',//腾讯-im
            'identifier'    =>   'user_'.$id,
        ];
        $userInfo= UserAuth::where($where)->first();
        return $this->success('success', $userInfo);
    }


    /**
     * User: 孟郝泽
     * DateTime: 2021/6/11 2:49 下午
     * Describe: 腾讯IM 患者端用户
     * Version: V1.0
     * @return \Illuminate\Http\JsonResponse
     */
    public function userAuthAdd() {
        $id = request()->input('id');
        $Adddata = [
            'user_id'       => $id,
            'identity_type' => 'tencent_im',
            'identifier'    =>   'user_'.$id,
        ];
        try {
            $user= UserAuth::where($Adddata)->first();
            if(empty($user)){
                $userInfo=  UserAuth::insertOrIgnore($Adddata);
            }else{
                return $this->error('error',"以及导入请勿重复！");
            }
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }
        return $this->success('success', $userInfo);
    }

}
